package com.example.palette.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.viewpager2.widget.ViewPager2;

import java.util.ArrayList;
import java.util.List;

public class CircleIndicatorView extends View {
    int mCount = 3;//指示器个数
    int mSpace = 40;//指示器间间隔
    int mRadius = 20;//指示器圆半径
    int mPaintStrokeWidth = 5;//像素
    Paint mPaint;
    List<CircleIndicator> mCircleIndicators;//指示器圆心集合
    int mSelecedPosition;//当前指示器索引
    indicatorClickListener mIndicatorClickListener;
    ViewPager2 mViewPager2;
    public CircleIndicatorView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setStrokeWidth(mPaintStrokeWidth);
        mPaint.setStyle(Paint.Style.FILL);
        mCircleIndicators = new ArrayList<>();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int width = mRadius * 2 * mCount + mPaintStrokeWidth * 2 * mCount + mSpace * (mCount - 1);
        int height = mRadius * 2 + mPaintStrokeWidth * 2;
        setMeasuredDimension(width,height);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        for (int i = 0; i < mCircleIndicators.size(); i++) {
            CircleIndicator circleIndicator = mCircleIndicators.get(i);
            if(i==mSelecedPosition){
                mPaint.setColor(Color.RED);
            }else {
                mPaint.setColor(Color.GREEN);
            }
            canvas.drawCircle(circleIndicator.mCx,circleIndicator.mCy,mRadius,mPaint);
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()){
            case MotionEvent.ACTION_UP:
                float x = event.getX();
                float y = event.getY();
                handleUp(x,y);
        }
        return true;
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        initCircleIndicators();
    }

    private void handleUp(float x, float y) {
        for (int i = 0; i < mCircleIndicators.size(); i++) {
            CircleIndicator circleIndicator = mCircleIndicators.get(i);
            if(x>=circleIndicator.mCx-mRadius-mPaintStrokeWidth && x<=circleIndicator.mCx+mRadius+mPaintStrokeWidth
                && y>=circleIndicator.mCy-mRadius-mPaintStrokeWidth && y<=circleIndicator.mCy+mRadius+mPaintStrokeWidth){
                if(mViewPager2 != null){
                    mViewPager2.setCurrentItem(i);
                }
                if(mIndicatorClickListener != null){
                    mIndicatorClickListener.onIndicatorClick(i);
                }
            }
        }
    }

    public void setUpViewPager2(ViewPager2 viewPager2){
        mViewPager2 = viewPager2;
        mViewPager2.unregisterOnPageChangeCallback(null);
        mViewPager2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {

            @Override
            public void onPageSelected(int position) {
                mSelecedPosition = position;
                invalidate();
            }
        });
    }
    public void setIndicatorClickListener(indicatorClickListener listener) {
        mIndicatorClickListener = listener;
    }

    private void initCircleIndicators() {
        mCircleIndicators.clear();
        float cx = 0;
        for (int i = 0; i < mCount; i++) {
            CircleIndicator circleIndicator = new CircleIndicator();
            if(i==0){
                cx = mRadius + mPaintStrokeWidth;
            }else {
                cx += (mRadius * 2 + mPaintStrokeWidth*2 + mSpace);
            }
            circleIndicator.mCx = cx;
            circleIndicator.mCy = getHeight()/2;
            mCircleIndicators.add(circleIndicator);
        }
    }

    public interface indicatorClickListener{
        void onIndicatorClick(int position);
    }

    private static class CircleIndicator{
        public float mCx;
        public float mCy;
    }
}
